import { Component, OnInit, ViewChild } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { STColumnTag, STColumn, STComponent, STColumnBadge } from '@delon/abc';
import { GridComponent, SupDic, HttpService } from 'lbf';
import { SFComponent, SFSchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-jgyj-jggz',
  templateUrl: './jggz.component.html',
  styleUrls: ['./jggz.less']
})

export class JgyjJggzComponent implements OnInit {
  addFormData = {};
  modiFormData = {};
  queryFormData = {}; 
  showAddWindow = false; 
  showModiWindow = false;
  @ViewChild('st', { static: true }) st: GridComponent;
  @ViewChild('addWindow', { static: false }) addWindow: SFComponent;
  @ViewChild('modiWindow', { static: false }) modiWindow: SFComponent;
  @ViewChild('searchForm', { static: false }) searchForm: SFComponent;

  TAG: STColumnTag = {
    1: { text: '是', color: 'green' },
    0: { text: '否', color: 'red' },
  };

  searchSchema: SFSchema = {
    properties: {
      JGZ001: {
        type: 'string',
        title: '规则启用标志',
        enum: this.dic.getSFDic('YESNO'),
        ui: {
          widget: 'select',
        }
      },
    },
    ui: {
      spanLabelFixed: 100,
      grid: {
        span: 6
      }
    }
  };

  gridData = [];
  columns: STColumn[] = [
    {
      title: '规则启用标志',
      index: 'JGZ001',
      render: 'custom'
    },
    { title: '规则类型', index: 'JGZ002' },
    { title: '类型名称', index: 'JGZ005' },
    { title: '时限(分)', index: 'JGZ003' },
    { title: '备注', index: 'AAE013' },
    {
      title: '操作区', buttons: [
        {
          text: '修改',
          icon: 'edit',
          click: (record: any) => this.modi(record),
        },
        {
          text: '删除',
          icon: 'delete',
          click: (record: any) => this.del(record),
          pop: true,
          popTitle: '确认要删除该规则吗？',
        },
      ]
    },
  ];

  addSchema = {
    properties: {
      JGZ001: {
        type: 'string',
        title: '规则启用标志',
        ui:
        {
          hidden: true
        }
      },
      JGZ002: {
        type: 'string',
        title: '规则类型'
      },
      JGZ005: {
        type: 'string',
        title: '类型名称'
      },
      JGZ003: {
        type: 'string',
        title: '时限(分)'
      },
      AAE013: {
        type: 'string',
        title: '备注'
      },
      JGZ004: {
        type: 'string',
        title: '规则序列',
        ui:
        {
          hidden: true
        }
      },
    },
    ui: {
      spanLabelFixed: 120,
      grid: {
        span: 24
      }
    },
    required: ['JGZ002', 'JGZ003', 'JGZ005']
  }

  modiSchema = {
    properties: {
      JGZ001: {
        type: 'string',
        title: '规则启用标志',
        enum: this.dic.getSFDic('YESNO'),
        ui: {
          widget: 'select',
        }
      },
      JGZ002: {
        type: 'string',
        title: '规则类型'
      },
      JGZ005: {
        type: 'string',
        title: '类型名称'
      },
      JGZ003: {
        type: 'string',
        title: '时限(分)'
      },
      AAE013: {
        type: 'string',
        title: '备注'
      },
      JGZ004: {
        type: 'string',
        title: '监管规则序列',
        ui:
        {
          hidden: true
        }
      },
    },
    ui: {
      spanLabelFixed: 120,
      grid: {
        span: 24
      }
    },
    required: ['JGZ002', 'JGZ003', 'JGZ005']
  }

  constructor(private http: _HttpClient,
    private dic: SupDic,
    private lbs: HttpService,
    public msgSrv: NzMessageService) { }
  ngOnInit() { }

  query() {
    // this.lbs.lbservice("ZSK_SXCX",{ para: this.searchForm.value }).then(resdata=>{
    //   if(resdata.code<1)
    //   {
    //       this.msgSrv.error(resdata.errmsg);
    //   }
    //   else
    //   {
    //       this.gridData=resdata.message.list;
    //   }

    // });
    this.st.reload(this.searchForm.value);
  }

  add() {
    this.showAddWindow = true;
  }

  handleCancel() {
    this.showAddWindow = false;
  }

  handleOk() {
    if (!this.addWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('jgyj_gzxz', { para: this.addWindow.value }).then(resdata => {
      console.log(this.addWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showAddWindow = false;
        this.addFormData = {};
        this.msgSrv.success('事项新增成功');
        this.query();
      }

    })
  }

  modi(rowData: any) {
    this.modiFormData = rowData;
    this.showModiWindow = true;
  }
  modiHandleCancel() {
    this.showModiWindow = false;
  }

  modiHandleOk() {
    if (!this.modiWindow.valid) {
      this.msgSrv.error('必填项不能为空！');
      return;
    }

    this.lbs.lbservice('jgyj_gzxg', { para: this.modiWindow.value }).then(resdata => {
      console.log(this.modiWindow.value);
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        this.showModiWindow = false;
        this.modiFormData = {};
        this.msgSrv.success('规则修改成功');
        this.query();
      }
    })
  }

  del(rowData: any) {
    this.lbs.lbservice("jgyj_gzsc", { para: { JGZ004: rowData.JGZ004 } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
      else {
        // this.showAddWindow = false;
        // this.addFormData = {};
        this.msgSrv.success('规则删除成功');
        this.query();
      }
    });
  }

  ngAfterViewInit() {
    this.query();
  }

  changeSxState(gzxl: string) {
    this.lbs.lbservice("jgyj_sxqt", { para: { JGZ004: gzxl } }).then(resdata => {
      if (resdata.code < 1) {
        this.msgSrv.error(resdata.errmsg);
      }
    });
  }
}
